﻿using System;
using System.Web;
using System.Web.UI;
using Microsoft.Practices.Unity;

namespace TSharp.Core.Web
{
    /// <summary>
    /// TSharp.Core.Web 所有PageBase的父类
    /// </summary>
    public abstract class AopBasePage : Page
    {
        /// <summary>
        /// 错误页面的路径
        /// </summary>
        public static string ErrorPagePath { get; set; }

        /// <summary>
        /// 重写初始化完成方法，加入ioc注入
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);
            InjectDependencies();
        }

        /// <summary>
        /// 注册消息的方法
        /// </summary>
        protected virtual void OnRegsiterMessage()
        {
        }


        /// <summary>
        /// Ico 注入
        /// </summary>
        protected virtual void InjectDependencies()
        {
            IUnityContainer container = AopContext.Default;
            if (container == null)
                throw new InvalidOperationException(Resource.NoRequestContainer);
            container.BuildUp(GetType(), this);
        }

        #region 页面方法重载

        /// <summary>
        /// 引发 <see cref="E:System.Web.UI.Control.Init"/> 事件以对页进行初始化。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs"/>。</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //this.Error += new System.EventHandler(this.PageBase_Error);

            OnRegsiterMessage();
        }

        /// <summary>
        /// 页面出发错误事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PageBase_Error(object sender, EventArgs e)
        {
            string errMsg;
            Exception currentError = Server.GetLastError();

            errMsg = "<h1>[系统错误]</h1>请联系系统管理员" +
                     "<hr>" +
                     "&nbsp;&nbsp;&nbsp;&nbsp;一个异常已发生，系统管理员已获取此异常信息．" +
                     "<br>" +
                     "[错误页面]: " + Request.Url + "<br>" +
                     "[错误消息]: <font class=\"Alert\">" + currentError.Message + "</font><br>" +
                     "[错误方法]:" + currentError.TargetSite + "<hr>" +
                     "";
            //			"<b> [堆栈记录]:</b><br/>"+
            //			currentError.ToString();

            ShowErrorPage(errMsg);
            Server.ClearError();
        }

        #endregion

        #region 错误处理

        /// <summary>
        /// 错误信息显示
        /// </summary>
        /// <param name="errorText"></param>
        public void ShowError(string errorText)
        {
            //待实现
        }

        /// <summary>
        /// 消息显示
        /// </summary>
        /// <param name="message"></param>
        public void ShowMessage(string message)
        {
            //待实现
        }


        /// <summary>
        /// 错误信息显示
        /// </summary>
        /// <param name="message"></param>
        protected void ShowErrorPage(string message)
        {
            Session["Error"] = message;

            if (string.IsNullOrEmpty(ErrorPagePath))
                ErrorPagePath = @"~/Error.aspx";
            Response.Redirect(ErrorPagePath, true);
        }

        #endregion

        #region  脚本处理

        /// <summary>
        /// 注册JS脚本块到客户端
        /// </summary>
        /// <param name="jsFileName">长地址</param>
        public void RegisterJS(string jsFileName)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(),
                                                        "<script language=\"JavaScript\" src=\"" + jsFileName +
                                                        "\"></script>\n");
        }

        /// <summary>
        /// 注册CSS到客户端
        /// </summary>
        /// <param name="cssFileName">Name of the CSS file.</param>
        public void RegisterCss(string cssFileName)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(),
                                                        "<LINK href=" + cssFileName +
                                                        " type=\"text/css\" rel=\"stylesheet\">\n");
        }


        /// <summary>
        /// 显示javascript
        /// </summary>
        /// <param name="_jscrip">javascript</param>
        public void ShowJavascript(string _jscrip)
        {
            string javascript;

            javascript = "<script language='javascript'>\n"
                         + "<!--\n"
                         + _jscrip
                         + "\n"
                         + "//-->\n"
                         + "</script>";
            Page.ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), javascript);
        }

        /// <summary>
        /// 显示javascript alert
        /// </summary>
        /// <param name="strAlert">alert内容</param>
        public void ShowJavascriptAlert(string strAlert)
        {
            strAlert = "alert('" + strAlert + "');";

            ShowJavascript(strAlert);
        }

        /// <summary>
        /// 显示javascript alert 并且返回
        /// </summary>
        /// <param name="strAlert">alert内容</param>
        public void ShowJavascriptAlertAndBack(string strAlert)
        {
            strAlert = "alert(\"" + strAlert + "\");"
                       + "history.go();  ";
            ShowJavascript(strAlert);
        }


        /// <summary>
        /// 显示javascript
        /// </summary>
        public void ShowJavascriptBack()
        {
            string strAlert = "history.back()";
            ShowJavascript(strAlert);
        }


        /// <summary>
        /// 显示javascript alert 并关闭当前窗口
        /// </summary>
        /// <param name="strAlert">alert内容</param>
        public void ShowJavascriptAlertAndClose(string strAlert)
        {
            string strJScript = "alert(\"" + strAlert + "\");"
                                + "close();";
            ShowJavascript(strJScript);
        }

        /// <summary>
        /// Shows the javascript alert close and flash opener.
        /// </summary>
        /// <param name="strAlert">The STR alert.</param>
        public void ShowJavascriptAlertCloseAndFlashOpener(string strAlert)
        {
            string strJScript = "alert(\"" + strAlert + "\");"
                                + "window.opener.location.href=window.opener.location.href; close();";
            ShowJavascript(strJScript);
        }

        /// <summary>
        /// 定位父窗口地址
        /// </summary>
        /// <param name="parentHref"></param>
        public void LocationParentHref(string parentHref)
        {
            string strJScript = string.Format("window.parent.location.href='{0}'; ", parentHref);
            ShowJavascript(strJScript);
        }

        /// <summary>
        /// 定位父窗口地址
        /// </summary>
        /// <param name="href">The href.</param>
        public void LocationHref(string href)
        {
            string strJScript = string.Format("window.location.href='{0}';", href);
            ShowJavascript(strJScript);
        }

        #endregion
    }
}